package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aixingfu.sports.Adapter.OrderListAdapter;
import com.aixingfu.sports.MVP.contract.OrderListContract;
import com.aixingfu.sports.MVP.presenter.OrderListPresenter;
import com.aixingfu.sports.Model.OrderListModel;
import com.aixingfu.sports.R;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orie.librarybase.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//订单列表
@Route(path = "/main/OrderListActivity")
public class OrderListActivity extends BaseActivity implements OrderListContract.MyView {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycle_order)
    RecyclerView recycleOrder;
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private int page = 1;
    private int number = 15;
    private OrderListPresenter orderListPresenter;
    private OrderListAdapter orderListAdapter;
    private List<OrderListModel.DataBeanX.DataBean> orderList=new ArrayList<>();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_order_list;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/OrderListActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setText("合同列表");
        orderListPresenter = new OrderListPresenter();
        orderListPresenter.attachView(this);
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        mSwipeRefreshLayout.setColorSchemeResources(R.color.textColor5, R.color.purple, R.color.yellow1);
        mSwipeRefreshLayout.setRefreshing(true);
        getOrder(1);
        recycleOrder.setLayoutManager(new LinearLayoutManager(OrderListActivity.this, LinearLayoutManager.VERTICAL, false));
        orderListAdapter = new OrderListAdapter(OrderListActivity.this, R.layout.item_order, orderList);
//        recycleOrder.addItemDecoration(new SpaceItemDecoration(0, 5));
//        //设置分割线
//        recycleList.addItemDecoration(new DividerItemDecoration(
//                getActivity(), DividerItemDecoration.VERTICAL));
        recycleOrder.setAdapter(orderListAdapter);
        orderListAdapter.setEmptyView(LayoutInflater.from(OrderListActivity.this).inflate(R.layout.empty_data, null, false));
        orderListAdapter.disableLoadMoreIfNotFullPage(recycleOrder);
        orderListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recycleOrder.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, "run==: 来了来了");
                        if (page * number == orderList.size()) {//还有数据
                            Log.e(TAG, "run==: 加载完这一次");
                            page = page + 1;
                            getOrder(2);
                        } else {//加载完了
                            orderListAdapter.loadMoreEnd();
                        }
                    }
                }, 100);
            }
        });
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 开始刷新，设置当前为刷新状态
                if (mSwipeRefreshLayout != null) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
                page = 1;
                if (orderList != null){
                    orderList.clear();
                }
                getOrder(1);
                // 加载完数据设置为不刷新状态，将下拉进度收起来
//                swipeRefreshView.setRefreshing(false);
            }
        });
        orderListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build("/main/ContractShowActivity").withString("order_id",orderList.get(position).getOrder_id()).withString("contract_id",orderList.get(position).getMember_deal_id()).navigation();
            }
        });

    }
    public void getOrder(int state){
        if (orderListPresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("page", String.valueOf(page));
            map.put("limit", String.valueOf(number));
            orderListPresenter.getOrderList(map,state);
        }
    }
    @Override
    protected void initEvent() {

    }

    @Override
    protected void beforeInit() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showToast(String msg, String state) {

    }

    @Override
    public void setOrderList(OrderListModel data, int state) {
        if (data.getCode() == 1) {
            if (state == 1) {
                orderList = data.getDataX().getDataX();
                orderListAdapter.setNewData(orderList);

                if (mSwipeRefreshLayout != null){
                    // 加载完数据设置为不刷新状态，将下拉进度收起来
                    mSwipeRefreshLayout.setRefreshing(false);
                }

            } else {
                orderList.addAll(data.getDataX().getDataX());
                orderListAdapter.setNewData(orderList);

                if (mSwipeRefreshLayout != null){
                    // 加载完数据设置为不刷新状态，将下拉进度收起来
                    mSwipeRefreshLayout.setRefreshing(false);
                }

            }
        }
    }

    @Override
    public void showLoading() {
showLoadingDialog();
    }

    @Override
    public void hideLoading() {
dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }
}
